package com.kbbywyclient.widget.sml;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * Created by Jiangzx on 2017/8/16 14:51.
 */

public class FooterLoadMore extends LinearLayout implements RefreshFooter {
    public static String REFRESH_FOOTER_PULL_UP = "上拉加载更多";
    public static String REFRESH_FOOTER_RELEASE = "释放立即加载";
    public static String REFRESH_FOOTER_LOADING = "正在加载...";
    public static String REFRESH_FOOTER_FINISH = "加载完成";
    public static String REFRESH_FOOTER_FAILED = "加载失败";
    public static String REFRESH_FOOTER_ALL_LOADED = "全部加载完成";

    private TextView textView;
    private int mTextColor=0xFF4BD2CC;//默认字体颜色
    private float mTextSize=14;//默认字体大小14sp
    private boolean mLoadMoreFinished = false;

    public FooterLoadMore(Context context) {
        this(context,null);
    }

    public FooterLoadMore(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FooterLoadMore(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textView=new TextView(context);
        textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        textView.setTextColor(mTextColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
        textView.setText(REFRESH_FOOTER_PULL_UP);
        if(attrs!=null){
            TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.FooterLoadMore);
            int count=array.length();
            int attr;
            for(int i=0;i<count;i++){
                attr=array.getIndex(i);
                switch (attr){
                    case R.styleable.FooterLoadMore_footerTextColor:
                        //文字颜色，默认为黑色
                        textView.setTextColor(array.getColor(attr,mTextColor));
                        break;
                    case R.styleable.FooterLoadMore_footerTextSize:
                        //文字大小，默认15sp
                        int fontSize=array.getDimensionPixelSize(attr,0);
                        if(fontSize>0){
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,fontSize);
                        }else{
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
                        }
                        break;
                }
            }
            array.recycle();
        }
        addView(textView);
        setGravity(Gravity.CENTER);
    }

    @Override
    public void onPullingUp(float percent, int offset, int footerHeight, int extendHeight) {

    }

    @Override
    public void onPullReleasing(float percent, int offset, int footerHeight, int extendHeight) {

    }

    @Override
    public boolean setLoadmoreFinished(boolean finished) {
        if(mLoadMoreFinished !=finished){
            mLoadMoreFinished = finished;
            if(finished){
                textView.setText(REFRESH_FOOTER_ALL_LOADED);
            }else{
                textView.setText(REFRESH_FOOTER_PULL_UP);
            }
        }
        return true;
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {

    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        if(!mLoadMoreFinished){
            if(success){
                textView.setText(REFRESH_FOOTER_FINISH);
            }else{
                textView.setText(REFRESH_FOOTER_FAILED);
            }
        }
        return 0;
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        if(!mLoadMoreFinished){
            switch (newState){
                case None:
                case PullToUpLoad:
                    textView.setText(REFRESH_FOOTER_PULL_UP);
                    break;
                case Loading:
                    textView.setText(REFRESH_FOOTER_LOADING);
                    break;
                case ReleaseToLoad:
                    textView.setText(REFRESH_FOOTER_RELEASE);
                    break;
            }
        }
    }

    public void setTextColor(@ColorInt int textColor){
        this.mTextColor=textColor;
        this.textView.setTextColor(mTextColor);
    }

    public void setTextSize(float textSize){
        this.mTextSize=textSize;
        this.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
    }
}
